import backtrader as bt
import pandas as pd
from datetime import datetime
 
 
def run_backtrader(strategy):
    cerebro = bt.Cerebro()
    # 增加一个策略
    cerebro.addstrategy(strategy)
    
    #获取数据
    stock_hfq_df = pd.read_excel("./sh110063.xlsx",index_col='date',parse_dates=True)
    start_date = datetime(2025, 6, 20)  # 回测开始时间
    end_date = datetime(2025, 6, 30)  # 回测结束时间
    data = bt.feeds.PandasData(dataname=stock_hfq_df, fromdate=start_date, todate=end_date)  # 加载数据
    cerebro.adddata(data)  # 将数据传入回测系统                                           
    cerebro.broker.setcommission(commission=0.00012)   
    cerebro.broker.setcash(100000.0)
    print('Starting Portfolio Value: %.2f' % cerebro.broker.getvalue())
 
    cerebro.run()
 
    print('Final Portfolio Value: %.2f' % cerebro.broker.getvalue())